﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.Msg;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.Core;
using FluentValidation.Results;

namespace Microhard.WarehouseInsp.Buz.Validator
{
    public class PledgePlanInRecordValidator : AbstractValidator<PledgePlanInRecordsModel>
    {
        public PledgePlanInRecordValidator()
        {
            RuleSet("Insert", () =>
           {
               RuleFor(u => u.ProductIDForSelect).NotEmpty().WithMessage(PledgePlanInRecordsMsg.NeedProduct);
               RuleFor(u => u.WarehouseIDForSelect).NotEmpty().WithMessage(PledgePlanInRecordsMsg.NeedWarehouse);

               RuleFor(u => u.PlanCount).GreaterThan(0).WithMessage(PledgePlanInRecordsMsg.NeedPlanCount);
               RuleFor(u => u.PlanPrice).GreaterThan(0).WithMessage(PledgePlanInRecordsMsg.NeedPlanPrice);

               Custom(u =>
               {
                   IWarehouse iwh = IoC.Resolve<IWarehouse>();
                   if (!iwh.IsExistByName(u.WarehouseIDForSelect))
                   {
                       return new ValidationFailure("WarehouseIDForSelect", PledgePlanInRecordsMsg.NeedWarehouse);
                   }
                   //质押物是否重复添加
                   IPledgePlanInRecords ipir = IoC.Resolve<IPledgePlanInRecords>();
                   if (ipir.HasAddPlg(u.ProjectID, u.ProductIDForSelect))
                   {
                       return new ValidationFailure("HasAddPlg", "HasAddPlg");
                   }
                   return null;
               });
           });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.ProductIDForSelect).NotEmpty().WithMessage(PledgePlanInRecordsMsg.NeedProduct);
                RuleFor(u => u.WarehouseIDForSelect).NotEmpty().WithMessage(PledgePlanInRecordsMsg.NeedWarehouse);

                RuleFor(u => u.PlanCount).GreaterThan(0).WithMessage(PledgePlanInRecordsMsg.NeedPlanCount);
                RuleFor(u => u.PlanPrice).GreaterThan(0).WithMessage(PledgePlanInRecordsMsg.NeedPlanPrice);

                Custom(u =>
                {
                    IWarehouse iwh = IoC.Resolve<IWarehouse>();
                    if (!iwh.IsExistByName(u.WarehouseIDForSelect))
                    {
                        return new ValidationFailure("WarehouseIDForSelect", PledgePlanInRecordsMsg.NeedWarehouse);
                    }
                    return null;
                });
            });

        }
    }
}
